// pages/metest/metest.js
import WxValidate from '../../utils/WxValidate'
import api from '../../api/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    oldpwdShow: true,
    newPwdShow:true,
    aPwdShow:true,
    statusBarHeight: '',
    form:{
      oldpwd:'',
      newPwd:'',
      aPwd:''
    }
  },
  goToMyBack(){
    wx.navigateBack()
  },
  getSysInfo() {
    let that = this
    let res = wx.getSystemInfoSync();
    that.setData({
      statusBarHeight: res.statusBarHeight
    })
    // console.log('状态栏高度', that.data.statusBarHeight)
  },
  //表单参数验证
initValidate(){
  const rules = {
    oldpwd: {
      required: true,
    },
    newPwd:{
      required: true,
      unequalTo: "oldpwd",
    },
    aPwd:{
      required: true,
      equalTo:'newPwd'

    },
  }
  const messages = {
    oldpwd:{
      required: '请输入旧密码',
    },
    newPwd:{
      required: '请输入新密码',
      maxlength: '新密码最多输入16位'
      
    },
    aPwd:{
      required: '请再次输入新密码',
      maxlength: '新密码最多输入16位'
    },
  }
  this.WxValidate = new WxValidate(rules, messages);

},
oldpwdchange(e){
  let that = this
  that.setData({
    oldpwd: e.detail
  })
},
newPwdchange(e){
  let that = this
  that.setData({
    newPwd: e.detail
  })
},
aPwdchange(e){
  let that = this
  that.setData({
    aPwd: e.detail
  })
},
onClickoldpwdIcon(event) {
  this.setData({
    oldpwdShow: !this.data.oldpwdShow
  });
},
onClicknewPwdIcon(event) {
  this.setData({
    newPwdShow: !this.data.newPwdShow
  });
},
onClickaPwdIcon(event) {
  this.setData({
    aPwdShow: !this.data.aPwdShow
  });
},
submitForm: function(){
  let that =this 
  const params ={
    oldpwd:that.data.oldpwd,
    newPwd:that.data.newPwd,
    aPwd:that.data.aPwd
  }
  //调用校验方法
  if(!that.WxValidate.checkForm(params)){
      const error = that.WxValidate.errorList[0];
      if(that.data.aPwd&&that.data.newPwd&&that.data.oldpwd&& that.data.newPwd!= that.data.aPwd){
        wx.showToast({
          icon:'none',
          title: '两次密码输入不同',
          duration: 2000//持续的时间
        })
      }else{
      //提示错误信息
      wx.showToast({
        icon:'none',
        title: error.msg,
        duration: 2000
       });
    }

      return false;
  }
  else{
    var userId = wx.getStorageSync('id')
    let passwordReq ={
      againPassword:that.data.aPwd,
      newPassword:that.data.newPwd,
      password:that.data.oldpwd,
      userId:userId
    }
    api.post('/appLogin/updatePassword', passwordReq).then(res => {
      if (res.code == 200){
        setTimeout(function() {
          wx.showToast({
            icon:'none',
            title: "修改成功",
            duration: 2000
         });
       }, 2000);
 
        wx.navigateBack()
      }else{
        wx.showToast({
          icon:'none',
          title: "修改失败",
          duration: 2000
       });
     }
  })
  }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getSysInfo()
    this.initValidate()

  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})